package server;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServletLoader {

    public static Map<String,HttpServlet> loadServlet(InputStream resource) {

        Map<String,HttpServlet> servletMap = new HashMap<>();

        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(resource);
            Element rootElement = document.getRootElement();

            List<Element> selectNodes = rootElement.elements("servlet");
            for (int i = 0; i < selectNodes.size(); i++) {
                Element element = selectNodes.get(i);
                // <servlet-name>lagou</servlet-name>
                Element servletnameElement = (Element) element.element("servlet-name");
                String servletName = servletnameElement.getStringValue();
                // <servlet-class>server.LagouServlet</servlet-class>
                Element servletclassElement = (Element) element.element("servlet-class");
                String servletClass = servletclassElement.getStringValue();
                // 根据servlet-name的值找到url-pattern
                List<Element> servletMappings = rootElement.elements("servlet-mapping");
                Map<String, String> servletUrlMap = new HashMap<>();
                for(Element servletMapping : servletMappings){
                    servletUrlMap.put(servletMapping.element("servlet-name").getStringValue(), servletMapping.element("url-pattern").getStringValue());
                }
                // /lagou
                String urlPattern = servletUrlMap.get(servletName);
                servletMap.put(urlPattern, (HttpServlet) Class.forName(servletClass).newInstance());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return servletMap;
    }

    public static Map<String,HttpServlet> loadServlet(InputStream resource, String classPath) {

        Map<String,HttpServlet> servletMap = new HashMap<>();

        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(resource);
            Element rootElement = document.getRootElement();

            List<Element> selectNodes = rootElement.elements("servlet");
            for (int i = 0; i < selectNodes.size(); i++) {
                Element element = selectNodes.get(i);
                // <servlet-name>lagou</servlet-name>
                Element servletnameElement = (Element) element.element("servlet-name");
                String servletName = servletnameElement.getStringValue();
                // <servlet-class>server.LagouServlet</servlet-class>
                Element servletclassElement = (Element) element.element("servlet-class");
                String servletClass = servletclassElement.getStringValue();
                // 根据servlet-name的值找到url-pattern
                List<Element> servletMappings = rootElement.elements("servlet-mapping");
                Map<String, String> servletUrlMap = new HashMap<>();
                for(Element servletMapping : servletMappings){
                    servletUrlMap.put(servletMapping.element("servlet-name").getStringValue(), servletMapping.element("url-pattern").getStringValue());
                }
                // /lagou
                String urlPattern = servletUrlMap.get(servletName);

                //通过 URLClassLoader 读取外部.class文件并实例化
                URLStreamHandler streamHandler = null;
                String repository = (new URL("file", null, classPath + File.separator)).toString();
                URLClassLoader loader = new URLClassLoader(new URL[]{
                        new URL(null,repository, streamHandler)
                });
                servletMap.put(urlPattern, (HttpServlet)loader.loadClass(servletClass).newInstance());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return servletMap;
    }


}
